package com.example.demo.controller;

import com.example.demo.annotion.ULog;
import com.example.demo.domin.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/queryAllById")
    @ULog
    public List<User> queryAllById(Long id) {

        List<User> result = userService.queryAllById(id);
        return result;
    }

    @PostMapping("/save")
    @ULog
    public String queryAllByName(@RequestBody User user) {

        userService.save(user);
        return "ok";
    }

    @GetMapping("/cookie/demo")
    public void cookieDemo(HttpServletRequest request, HttpServletResponse response){

        Cookie cookie = new Cookie("user","by");
        response.addCookie(cookie);

    }

    @GetMapping("/getcookie")
    public void getCookieDemo(HttpServletRequest request, HttpServletResponse response){

        HttpSession session = request.getSession(true);
        session.getId();
        session.setAttribute("user","by");
        Object value = session.getAttribute("user");
        System.out.println(value);

    }

    @RequestMapping(value="/ulog")
    @ULog
    public String test(){
        return "hello world";
    }

}
